package com.member.mp.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.core.domain.R;
import com.member.mp.web.user.SupperController;
import com.member.shop.domain.SysBizGoodsRank;
import com.member.shop.domain.SysBizRankList;
import com.member.shop.dto.GoodsQueryDto;
import com.member.shop.service.ISysBizGoodsRankService;
import com.member.shop.service.ISysBizGoodsService;
import com.member.shop.service.ISysBizRankListService;
import com.member.shop.vo.CategoryVo;
import com.member.shop.vo.GoodsIndexVo;
import com.member.shop.vo.RankInfoVo;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【榜单】控制器
 *
 * @author wangxin
 * @version 1.0
 * @date Mon Jul 03 22:06:51 CST 2024
 **/
@RestController
@Api(value = "榜单接口", tags = {"榜单接口"})
@RequestMapping("/wx/rank")
public class RankController extends SupperController {


    @Resource
    private ISysBizGoodsRankService goodsRankService;
    @Resource
    private ISysBizRankListService rankListService;
    @Resource
    private ISysBizGoodsService sysBizGoodsService;

    /**
     * 查询榜单信息
     *
     * @param id 榜单id
     * @return 榜单信息
     */
    @GetMapping("/queryInfo/{id}")
    public R<RankInfoVo> queryInfo(@PathVariable("id") Long id) {
        SysBizRankList sysBizRankList = rankListService.getById(id);
        RankInfoVo rankInfoVo = new RankInfoVo();
        BeanUtils.copyProperties(sysBizRankList, rankInfoVo);
        List<CategoryVo> categoryList = new ArrayList<>();
        rankInfoVo.setCategoryVoList(categoryList);

        List<GoodsIndexVo> goodsList = new ArrayList<>();

        if (!CollectionUtils.isEmpty(categoryList)) {
            Long categoryId = categoryList.get(0).getId();
            goodsList = getGoodsIndexVos(categoryId, id);
        }
        rankInfoVo.setGoodsVoList(goodsList);

        return R.ok(rankInfoVo);
    }

    /**
     * 查询榜单商品信息
     *
     * @param id         榜单id
     * @param categoryId 商品分类id
     * @return 榜单信息
     */
    @GetMapping("/queryGoodsList")
    public R<List<GoodsIndexVo>> queryGoodsList(@RequestParam("id") Long id, @RequestParam("categoryId") Long categoryId) {
        List<GoodsIndexVo> goodsList = getGoodsIndexVos(categoryId, id);
        return R.ok(goodsList);
    }

    private List<GoodsIndexVo> getGoodsIndexVos(Long categoryId, Long rankId) {
        List<GoodsIndexVo> goodsList = new ArrayList<>();
        GoodsQueryDto goodsQueryDto = new GoodsQueryDto();
        goodsQueryDto.setCategoryId(categoryId);
        goodsQueryDto.setTenantId(getTenantId());
        LambdaQueryWrapper<SysBizGoodsRank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysBizGoodsRank::getRankId, rankId);

        List<SysBizGoodsRank> list = goodsRankService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(list)) {
            List<Long> ids = list.stream().map(SysBizGoodsRank::getGoodsId).collect(Collectors.toList());
            goodsList = sysBizGoodsService.selectSysBizGoodsByIds(goodsQueryDto, ids);
        }

        return goodsList;
    }

}
